<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .accordion {
      width: 300px;
      height: 40px;
      background-color: #F2F2F2;
      border: 0;
      text-align: left;
    }

    .accordion:hover {
      text-decoration: underline
    }

    .content {
      max-height: 0;
      overflow: hidden;
    }
  </style>
</head>

<body>


  <button class="accordion">#1测试内容</button>
  <div class="content">
    <p>1的内容,随便写写</p>
    <p>1的内容,随便写写</p>
    <p>1的内容,随便写写</p>
  </div>
  <button class="accordion">#2测试内容</button>
  <div class="content">
    <p>2的内容,随便写写</p>
    <p>2的内容,随便写写</p>
  </div>
  <button class="accordion">#3测试内容</button>
  <div class="content">
    <p>3的内容,随便写写</p>
  </div>
  <script>
    var arr = document.getElementsByClassName('accordion')
    for (var a = 0; a < arr.length; a++) {
      //添加监听事件只能用于按钮
      arr[a].addEventListener('click', function () {
        //返回指定元素之后的下一个兄弟元素
        var panel = this.nextElementSibling;
        //当他等于上面的.content点击的时候就变为空
        if (panel.style.maxHeight) {
          panel.style.maxHeight = null;
        }
        //当他不等于上面那个.content点击的时候就让他显示.content
        else {
          // 它返回该元素的像素高度，高度包含内边距
          panel.style.maxHeight = panel.scrollHeight + "px";
        }

      })
    }

  </script>
</body>

</html>